package conductores;

import java.util.ArrayList;

import aima.search.framework.Successor;

public class OpMove implements IOperator {

	@Override
	public void execute(Board board, int condO, Conductor conductorOrigen,
			ArrayList<Object> successors) {

		boolean zero = false;
		for (Integer usernum : conductorOrigen.getRuta()) {
			if (usernum > 0) {
				Move(board, condO, conductorOrigen, usernum, successors);
			} else if (!zero && usernum == 0) {
				zero = true;
				Move(board, condO, conductorOrigen, usernum, successors);
			}
		}

	}

	private void Move(Board board, int condO, Conductor conductorOrigen,
			Integer usernum, ArrayList<Object> successors) {

		for (int condD = 0; condD < board.getConductores().size(); condD++) {
			Conductor conductorDesti = board.getConductores().get(condD);
			if (!conductorOrigen.equals(conductorDesti)) {
				Board newboard = board.clone();
				Conductor ncondO = newboard.getConductores().get(condO);
				Conductor ncondD = newboard.getConductores().get(condD);
				if (newboard.Move(ncondO, ncondD, usernum)) {
					String s = "Movemos el usuario " + usernum
							+ " del conductor " + conductorOrigen.identificador
							+ " al conductor " + conductorDesti.identificador;
					successors.add(new Successor(s, newboard));
				}
			}
		}
	}
}
